package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;


    public PageResult<BrandDTO> queryBrandPage(
            String key,
            Integer page,
            Integer rows,
            String sortBy,
            Boolean desc) {
        //设置分页参数
        PageHelper.startPage(page,rows);

        //拼装查询条件
        Example example = new Example(Brand.class);

        //判断是否有关键字
        if (StringUtils.isNotBlank(key)){
            Example.Criteria criteria = example.createCriteria()
                    .orLike("name", "%" + key + "%")
                    .orEqualTo("letter", key);
        }

        //排序: SELECT * FROM tb_brand WHERE letter = 'X' ORDER BY id DESC
//        example.setOrderByClause(" id DESC");
        example.setOrderByClause(sortBy + (desc ? " DESC" : " ASC"));

        //执行查询
        List<Brand> brandList = brandMapper.selectByExample(example);

        if(CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
        long total = pageInfo.getTotal();
        List<Brand> list = pageInfo.getList();

        List<BrandDTO> brandDTOS = BeanHelper.copyWithCollection(list, BrandDTO.class);

        PageResult<BrandDTO> pageResult = new PageResult<>(brandDTOS, total);

        return pageResult;
    }

    @Transactional //添加事务
    public void saveBrand(BrandDTO brandDto, List<Long> cids) {
        //保存品牌获取到的品牌
        Brand brand = BeanHelper.copyProperties(brandDto, Brand.class);
        brand.setId(null);
        int count = brandMapper.insertSelective(brand);
        if (count != 1){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        //自定义批量插入
        Long brandId = brand.getId();
        count = brandMapper.insertBrandCategory(brandId,cids);
        if (count != cids.size()){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 根据品牌id获取品牌信息
     * @param brandId   品牌id
     * @return  品牌信息
     */
    public BrandDTO queryBrandById(Long brandId) {
        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        if (brand == null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand,BrandDTO.class);
    }

    /**
     * 根据分类cid批量获取品牌信息(通过tb_category_brand中间表进行查询, 自定义多表联查sql语句查询)
     * @param cid
     * @return  品牌信息集合
     */
    public List<BrandDTO> queryBrandListByCategoryId(Long cid) {
        List<Brand> list = brandMapper.queryByCategoryId(cid);
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,BrandDTO.class);
    }

    /**
     *  批量查询品牌DTO
     * @param brandIds  品牌id集合
     * @return
     */
    public List<BrandDTO> queryBrandByIds(List<Long> brandIds) {
        List<Brand> brandList = brandMapper.selectByIdList(brandIds);
        if (CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brandList,BrandDTO.class);
    }
}
